<template>
    <div>
        <!--<HomeHeader :city="city"></HomeHeader>-->
        <HomeHeader></HomeHeader>
        <HomeSwiper :swiperList="swiperList"></HomeSwiper>
        <HomeIcons :iconList="iconList"></HomeIcons>
        <HomeRecommend :recommendList="recommendList"></HomeRecommend>
        <HomeWeekend :weekendList="weekendList"></HomeWeekend>
    </div>
</template>

<script>
    import axios from 'axios'
    import { mapState } from 'vuex'
    import HomeHeader from './components/Header'
    import HomeSwiper from './components/Swiper'
    import HomeIcons from './components/Icons'
    import HomeRecommend from './components/Recommend'
    import HomeWeekend from './components/Weekend'
    export default {
        name: "Home",
        data(){
            return {
                lastCity:'',
                swiperList:[],
                iconList:[],
                recommendList:[],
                weekendList:[]
            }
        },
        computed:{
            ...mapState(['city'])
        },
        components:{
            HomeHeader,
            HomeSwiper,
            HomeIcons,
            HomeRecommend,
            HomeWeekend
        },
        mounted(){
            //  页面初次渲染时执行
            //  console.log('mounted');
            this.lastCity = this.city;
            this.getHomeInfo()
        },
        activated(){
            //  页面内容重新显示(发生更改)时执行
            //  console.log('activated');
            if(this.lastCity !== this.city){
                this.lastCity = this.city;
                this.getHomeInfo()
            }
        },
        methods:{
            getHomeInfo(){
                axios.get('/api/index.json?city = ' + this.city)
                    .then(this.getHomeInfoSucc)
            },
            getHomeInfoSucc(res){
               res = res.data;
               if (res.ret && res.data){
                   const data = res.data;
                   //   this.city = data.city;
                   this.swiperList = data.swiperList;
                   this.iconList = data.iconList;
                   this.recommendList = data.recommendList;
                   this.weekendList = data.weekendList
               }
            }
        }
    }
</script>

<style scoped>

</style>